package org.gitman.filmBase.controller;

import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.gitman.filmBase.controller.actions.AbstractAction;
import org.gitman.filmBase.model.Model;
import org.gitman.filmBase.view.MainFrame;
import org.gitman.filmBase.view.View;

public class Controller {
	private Model model;
	private View view;
	private static Logger logger = Logger.getLogger(Controller.class);
	private Map<Class<? extends AbstractAction>, AbstractAction> actions;
	
	public Controller(){
		actions = new HashMap<Class<? extends AbstractAction>, AbstractAction>();	
		
		model = new Model();
		view = new View(this);
		model.setView(view);
		view.setModel(model);
		
		view.start();		
	}
	
	public Model getModel() {
		return model;
	}
	public void setModel(Model model) {
		this.model = model;
	}
	public View getView() {
		return view;
	}
	public void setView(View view) {
		this.view = view;
	}

	public void stop() {
		view.stop();
	}

	public ActionListener getActionListener(Class clazz, MainFrame mainFrame){
		AbstractAction al = actions.get(clazz);
		if(al == null){
			try {
				al = (AbstractAction) clazz.newInstance();
				al.setMainFrame(mainFrame);
				al.setController(this);
			} catch (Exception e) {
				logger.error("wrong Action!", e);
			}			
		}
		return al;
	}
}
